<template>
  <el-card class="application-list-card">
    <h2>收到的岗位申请</h2>
    <el-form :inline="true" :model="searchForm" class="search-form" @submit.prevent>
      <el-form-item label="状态">
        <el-select v-model="searchForm.status" clearable placeholder="全部">
          <el-option label="待处理" :value="0" />
          <el-option label="已拒绝" :value="1" />
          <el-option label="已邀请面试" :value="2" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="fetchApplications">筛选</el-button>
        <el-button @click="resetSearch">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="applications" stripe border class="application-table" v-loading="loading">
      <el-table-column prop="positionName" label="岗位名称" min-width="120" />
      <el-table-column label="学生姓名" min-width="100">
        <template #default="{ row }">
          <el-link type="primary" @click="showStudentInfo(row.studentId)">{{ studentNameMap[row.studentId] || '点击查看' }}</el-link>
        </template>
      </el-table-column>
      <el-table-column prop="applyTime" label="投递时间" min-width="140" />
      <el-table-column prop="status" label="状态" min-width="100">
        <template #default="{ row }">
          <el-tag :type="statusTagType(row.status)">{{ statusMap[convertStatusToNumber(row.status, 'application')] }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="remark" label="备注" min-width="120" />
      <el-table-column label="操作" min-width="220">
        <template #default="{ row }">
          <el-button v-if="convertStatusToNumber(row.status, 'application') === 0" type="success" size="small" @click="accept(row.id)">邀请面试</el-button>
          <el-button v-if="convertStatusToNumber(row.status, 'application') === 0" type="danger" size="small" @click="reject(row.id)">拒绝</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination-bar">
      <el-pagination
        background
        layout="prev, pager, next, jumper"
        :total="total"
        :page-size="searchForm.size"
        :current-page="searchForm.page"
        @current-change="onPageChange"
      />
    </div>
    <el-dialog v-model="showInvite" title="发起面试邀请" width="500px">
      <el-form :model="inviteForm" label-width="100px">
        <el-form-item label="面试时间">
          <el-date-picker v-model="inviteForm.interviewTime" type="datetime" value-format="YYYY-MM-DD HH:mm:ss" />
        </el-form-item>
        <el-form-item label="面试地点">
          <el-input v-model="inviteForm.interviewLocation" />
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="inviteForm.remark" type="textarea" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="showInvite = false">取消</el-button>
        <el-button type="primary" @click="submitInvite" :loading="inviteLoading">确定邀请</el-button>
      </template>
    </el-dialog>
    <el-dialog v-model="showStudentDialog" title="学生详细信息" width="500px">
      <el-descriptions v-if="studentDetail" :column="1" border>
        <el-descriptions-item label="姓名">{{ studentDetail.name }}</el-descriptions-item>
        <el-descriptions-item label="性别">{{ studentDetail.gender === 1 ? '男' : '女' }}</el-descriptions-item>
        <el-descriptions-item label="出生日期">{{ studentDetail.birthDate }}</el-descriptions-item>
        <el-descriptions-item label="手机号">{{ studentDetail.phone }}</el-descriptions-item>
        <el-descriptions-item label="邮箱">{{ studentDetail.email }}</el-descriptions-item>
        <el-descriptions-item label="学校">{{ studentDetail.school }}</el-descriptions-item>
        <el-descriptions-item label="专业">{{ studentDetail.major }}</el-descriptions-item>
        <el-descriptions-item label="学历">{{ studentDetail.degree }}</el-descriptions-item>
        <el-descriptions-item label="毕业时间">{{ studentDetail.graduateDate }}</el-descriptions-item>
        <el-descriptions-item label="项目经历">{{ studentDetail.projectExperience }}</el-descriptions-item>
        <el-descriptions-item label="获奖情况">{{ studentDetail.award }}</el-descriptions-item>
        <el-descriptions-item label="证书">{{ studentDetail.certificate }}</el-descriptions-item>
      </el-descriptions>
    </el-dialog>
  </el-card>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import {
  getEnterpriseApplications,
  rejectApplication,
  acceptApplication,
  getStudentInfo
} from '@/api/enterprise'
import { convertStatusToNumber } from '@/utils/enum'
import { ElLink } from 'element-plus'

const searchForm = ref({ status: null, page: 1, size: 10 })
const applications = ref([])
const total = ref(0)
const loading = ref(false)
const statusMap = {
  0: '待处理',
  1: '已拒绝',
  2: '已邀请面试'
}
const statusTagType = status => {
  const numStatus = convertStatusToNumber(status, 'application')
  if (numStatus === 0) return 'info'
  if (numStatus === 1) return 'danger'
  if (numStatus === 2) return 'success'
  return ''
}
const showInvite = ref(false)
const inviteForm = ref({ interviewTime: '', interviewLocation: '', remark: '' })
const inviteLoading = ref(false)
let currentAppId = null
const showStudentDialog = ref(false)
const studentDetail = ref(null)
const studentNameMap = ref({})
const allApplications = ref([])

const fetchApplications = async () => {
  loading.value = true
  try {
    const params = { ...searchForm.value }
    if (params.status === null || params.status === '') delete params.status
    // 不传分页参数，获取全部
    delete params.page
    delete params.size
    const res = await getEnterpriseApplications(params)
    const data = res.data || res
    allApplications.value = data.list || data.records || []
    total.value = allApplications.value.length
    // 默认显示第一页
    const start = (searchForm.value.page - 1) * searchForm.value.size
    const end = start + searchForm.value.size
    applications.value = allApplications.value.slice(start, end)
  } catch (e) {}
  loading.value = false
}
const onPageChange = page => {
  searchForm.value.page = page
  // 只切片，不重新请求
  const start = (searchForm.value.page - 1) * searchForm.value.size
  const end = start + searchForm.value.size
  applications.value = allApplications.value.slice(start, end)
}
const resetSearch = () => {
  searchForm.value = { status: null, page: 1, size: 10 }
  fetchApplications()
}
const accept = id => {
  currentAppId = id
  showInvite.value = true
}
const submitInvite = async () => {
  inviteLoading.value = true
  try {
    await acceptApplication(currentAppId, inviteForm.value)
    ElMessage.success('面试邀请已发送')
    showInvite.value = false
    fetchApplications()
  } catch (e) {}
  inviteLoading.value = false
}
const reject = async id => {
  try {
    await rejectApplication(id)
    ElMessage.success('已拒绝该申请')
    fetchApplications()
  } catch (e) {}
}
const showStudentInfo = async (id) => {
  if (!id) return
  const res = await getStudentInfo(id)
  studentDetail.value = res
  showStudentDialog.value = true
  // 缓存姓名
  if (res && res.name) studentNameMap.value[id] = res.name
}
onMounted(() => {
  fetchApplications()
})
</script>

<style scoped>
.application-list-card {
  max-width: 1100px;
  margin: 40px auto;
  padding: 30px 20px;
}
.search-form {
  margin-bottom: 20px;
}
.application-table {
  margin-bottom: 20px;
}
.pagination-bar {
  text-align: right;
  margin-top: 10px;
}
</style>